package cn.itsource.ymcc.valid;

import org.springframework.stereotype.Service;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class PhoneValidator implements ConstraintValidator<Phone, String> {

    private static final String PHONE_REGEX = "^1[3-9]{1}[0-9]{9}$";

    @Override
    public void initialize(Phone constraintAnnotation) {
       //你也可以写点  ，但是不想写
    }

    /**
     *
     * @param value 注解只要加在那个字段上面就是获取那个字段的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        //验证手机号 要符合PHONE_REGEX
        Pattern p = Pattern.compile(PHONE_REGEX);
        Matcher m = p.matcher(value);
        return m.matches();
    }
}
